package com.xwli.springcloud.service.impl;

import com.alibaba.fastjson.JSON;
import com.xwli.springcloud.dao.ServiceOrderTestRepository;
import com.xwli.springcloud.model.PaeQueryTestSvcReqEntity;
import com.xwli.springcloud.projo.ServiceOrderTest;
import com.xwli.springcloud.service.ServiceOrderTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: soc-demo
 * @description:
 * @author: fsp
 * @create: 2023-02-19 20:53
 **/
@Slf4j
@Service
public class ServiceOrderTestServiceImpl implements ServiceOrderTestService {

    @Autowired
    private ServiceOrderTestRepository serviceOrderTestRepository;

    @Override
    public ResponseEntity<List<ServiceOrderTest>> pageQueryTestSvc(PaeQueryTestSvcReqEntity paeQueryTestSvcReqEntity) {
        List<ServiceOrderTest> serviceOrderTestList = new ArrayList<>();
        try {
            //排序
            Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
            //分页
            Pageable pageable = PageRequest.of(paeQueryTestSvcReqEntity.getPageIndex()-1, paeQueryTestSvcReqEntity.getPageSize(), sort);
            //分页查询
            Page<ServiceOrderTest> serviceOrderTestPage = serviceOrderTestRepository.findAll(pageable);
            serviceOrderTestList = serviceOrderTestPage.getContent();
            log.info("响应:{}", JSON.toJSONString(serviceOrderTestList));
        }catch (Exception e){
            log.error("运行异常:",e);
        }
        return ResponseEntity.ok(serviceOrderTestList);
    }

    @Override
    public ResponseEntity<Integer> orderCountTestSvc() {
        return ResponseEntity.ok(serviceOrderTestRepository.findAll().size());
    }
}
